package com.itcast.monkey.web.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

import com.itcast.monkey.domain.base.Courier;
import com.itcast.monkey.service.base.CourierService;
import com.itcast.monkey.web.action.common.BaseAction;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @author 程侃
 * @project_name monkey_bos_management
 * @data 2018年3月7日下午11:55:59
 */
public class CourierAction extends BaseAction<Courier>{

	private int page;
	private int rows;
	private String ids;

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	@Autowired
	private CourierService courierService;

	@Action(value = "courier_save", results = {
			@Result(name = "success", location = "./pages/base/courier.html", type = "redirect") })
	public String save() {
		courierService.save(model);
		return SUCCESS;
	}


	@Action(value = "courier_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {
		Pageable pageable = new PageRequest(page - 1, rows);
		Specification<Courier> specification = new Specification<Courier>() {
			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<>();
				if (!StringUtils.isEmpty(model.getCourierNum())) {
					Predicate p1 = cb.equal(root.get("courierNum"), model.getCourierNum());
					list.add(p1);
				}
				;
				if (!StringUtils.isEmpty(model.getCompany())) {
					Predicate p2 = cb.like(root.get("company").as(String.class), "%" + model.getCompany() + "%");
					list.add(p2);
				}
				;
				if (!StringUtils.isEmpty(model.getTelephone())) {
					Predicate p3 = cb.equal(root.get("telephone"), model.getTelephone());
					list.add(p3);
				}
				;
				Join<Object, Object> join = root.join("standard", JoinType.INNER);
				if (model.getStandard() != null && !StringUtils.isEmpty(model.getStandard().getName())) {
					Predicate p4 = cb.like(join.get("name").as(String.class),
							"%" + model.getStandard().getName() + "%");
					list.add(p4);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		Page<Courier> list = courierService.pageQuery(specification, pageable);
		this.pushValueStack(list);
		return SUCCESS;
	}

	@Action(value = "courier_delBatch", results = {
			@Result(name = "success", type = "redirect", location = "./pages/base/courier.html") })
	public String delBatch() {
		String[] list = ids.split(",");
		courierService.delBatch(list);
		return SUCCESS;
	}
		
	@Action(value = "courier_findnoassociation", results = { @Result(name = "success", type = "json") })
	public String findnoassociation() {
		List<Courier> list = courierService.findnoassociation();
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
}
